<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_ad_text.php');
class AdTextControl
{
	var $ad_text_m;
	function __construct()
	{
	
	$this->ad_text_m = new AdTextModule();
	}
	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @param   int        $w_type     网站类型
	* @return  array
	*/
	function get_url_info($page,$type,$pagesize,$w_type,$search_array = null)
	{
		$url_str = '';
		if($type == 'list_index')
		{ 
			$sql = "SELECT COUNT(*) AS total FROM ad  WHERE  is_text_ad = 1 AND ad_web  = $w_type AND `is_delete`=0   ";
		}else if($type == 'list_s')
		{
			$where_str = empty($search_array['ad_name'])?'':" AND   ad_name LIKE '%".$search_array['ad_name']."%'";
			$url_str = '&url_page=1'; 
			$url_str .= empty($search_array['ad_name'])?'':'&ad_name='.urlencode($search_array['ad_name']);
			$url_str.='&w_type='.$w_type;
			$sql = "SELECT COUNT(*) AS total FROM ad  WHERE  is_text_ad = 1 AND is_delete = 0 AND ad_web  = $w_type  $where_str  ";
		}
		$page_info = get_page_info($sql,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表的 ad信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   array        $search_array   搜索数组
	* @return  array
	*/
	function  get_ad_info($type,$start = 0,$pagesize = 10,$w_type,$search_array = null)
	{
	
		if($type == 'list_index')
		{
			$sql = "SELECT ad_id,ad_url,ad_name,ad_content ,ad_user_update, IF(ad_update > 0, FROM_UNIXTIME(ad_update, '%Y-%m-%d %H:%s'), '') ad_update FROM 
			ad  WHERE is_text_ad = 1 AND is_delete = 0  AND ad_web = $w_type    ORDER BY ad_update  DESC  LIMIT $start,$pagesize ";
		}else
		{
			$where_str = empty($search_array['ad_name'])?'':" AND   ad_name LIKE '%".$search_array['ad_name']."%'";
			$sql = "SELECT ad_id,ad_url,ad_content,ad_name ,ad_user_update, IF(ad_update > 0, FROM_UNIXTIME(ad_update, '%Y-%m-%d %H:%s'), '') ad_update FROM 
			ad  WHERE is_text_ad = 1 AND is_delete = 0 AND ad_web = $w_type   $where_str   ORDER BY ad_update  DESC  LIMIT $start,$pagesize ";
		
		}
		
		$data =  $this->ad_text_m->get_ad_info($sql);
		foreach ($data AS $key => $values)
		{
			$sql = 'SELECT product.product_name  FROM ad_delivery delivery 
			LEFT JOIN won_product won_p   ON won_p.id = delivery.ws_id  
			LEFT JOIN product    ON won_p.won_product_id = product.product_id  WHERE  delivery.ad_id =  '.$values['ad_id'];
			$result = $GLOBALS['db']->query($sql);
			$product_name_str = '';
			while($row = $GLOBALS['db']->fetch_array($result))
			{
				$product_name_str.= empty($product_name_str)?$row['product_name']:'<br>'.$row['product_name'];
			}
			$data[$key]['product_name_str'] = $product_name_str;
		}
		return $data;
	}
	/**
	* 根据id得到ad信息
	* @param   int        $w_type 网站类型
	* @param   int         $ad_id
	* @return  array
	*/
	function  get_ad_id_info($ad_id,$w_type = 0)
	{
		$data = array();$product_id_str = $product_name_str = '';
		$sql = "SELECT ad_id,ad_url,ad_content,ad_name FROM ad WHERE ad_id = $ad_id ";
		$row =  $GLOBALS['db']->getAll($sql);
		
		$sql = "SELECT delivery.ws_id,product.product_name  FROM ad_delivery delivery 
		LEFT JOIN won_product won_p   ON won_p.id = delivery.ws_id  
		LEFT JOIN product    ON won_p.won_product_id = product.product_id  WHERE  delivery.ad_id = $ad_id ";
		
		$result = $GLOBALS['db']->query($sql);
		
		while($row1 = $GLOBALS['db']->fetch_array($result))
		{
			$product_id_str .=  empty($product_id_str)?$row1['ws_id']:','.$row1['ws_id'];
			$product_name_str .=  empty($product_name_str)?$row1['product_name']:','.$row1['product_name'];
		}
		unset($row1);
		$row[0]['product_id_str'] = $product_id_str;  $row[0]['product_name_str'] = $product_name_str;  
		return $row[0];
	}
	/**
	* 增加或者修改ad
	*
	* @param   string      $type            操作类型
	* @param   string      array     	      
	* @param   text        $content         内容
	* @param   int         $ad_id           广告id
	* @return  int
	*/
	function  operate_ad($type,$array = '',$w_type = 0,$ad_id = 0)
	{
		$operate_id = 0;
		$ad_id = $array['ad_id'];
		if($type == 'add')
		{
			$sql = "INSERT INTO  ad(ad_url,ad_content,ad_name,is_text_ad,ad_user_add,ad_addtime,ad_user_update,ad_update,ad_web ) 
			VALUES  ('{$array['ad_url']}','{$array['ad_content']}','{$array['ad_name']}',1,
			'".$_SESSION['AName']."','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."','".$_SERVER['REQUEST_TIME']."',$w_type)";
			$GLOBALS['db']->query($sql);
			$operate_id = $GLOBALS['db']->insert_id();
			$ad_id = $operate_id;
		}
		elseif($type == 'edit')
		{
			$sql = "UPDATE ad SET ad_url = '{$array['ad_url']}',ad_content = '{$array['ad_content']}',ad_name = '{$array['ad_name']}',
			ad_user_update = '".$_SESSION['AName']."' , ad_update =  '".$_SERVER['REQUEST_TIME']."',ad_web = $w_type  WHERE ad_id = ".$ad_id;
			$operate_id =  $GLOBALS['db']->query($sql);
			if(!empty($array['ad_start_products_position']))
			{
				$sql = "DELETE FROM ad_delivery WHERE is_text_ad = 1 AND ad_web = $w_type AND  FIND_IN_SET(ws_id,'{$array['ad_start_products_position']}') ";
				$GLOBALS['db']->query($sql);
			}
			
		}
		if(!empty($array['ad_products_position']))
		{
			$check_products = explode(',',$array['ad_products_position']);
			foreach ($check_products AS $key=>$values)
			{
				$sql = "INSERT INTO  ad_delivery (ad_web,ad_id,ws_id,is_text_ad,add_user,add_time,update_user,update_time)
				VALUES ($w_type,'{$ad_id}','{$values}',1,'".$_SESSION['AName']."','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName'].
				"','".$_SERVER['REQUEST_TIME']."')";
				$GLOBALS['db']->query($sql);
			}
		}
		return $operate_id;
	
	}
	
	/**
	* 增加或者修改投放
	*
	* @param   int        $w_type            网站类型
	* @param   in  $ad_products_position     产品id组合
	* @param   int         $ad_id            广告id
	* @param   string     $products_name     产品名称
	* @return  int
	*/
	function  get_products_list($w_type = 0,$ad_products_position = '',$ad_id = 0,$products_name = '')
	{
		$products_data = array();
		$check_products = explode(',',$ad_products_position);
		$where_str = empty($products_name)?'':" WHERE  Field_20045 LIKE '%".$products_name."%'";
		$sql  = "SELECT Field_20044 , Field_20045 FROM Table_26 $where_str ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			$check_str = '';
			if(in_array($row['Field_20044'],$check_products))
			{
				$check_str = 'checked="checked"';
			}
			$row['checkbox'] = '<input type="checkbox" name="products_list"  
			id="products_'.$row['Field_20044'].'" '.$check_str.' value="'.$row['Field_20044'].'" title = "'.$row['Field_20045'].'" >';
			$products_data[] = $row;
		}
		return $products_data;
	}
	/**
	* 批量删除标签
	*
	* @param   string         $list_id    id字符串
	*
	* @return  int
	*/
	function delete_ad($list_id)
	{
		$sql = "UPDATE ad SET is_delete=1  WHERE ad_id IN ($list_id) ";
		
		return $this->ad_text_m->change_info($sql);
	}


}





?>